package com.yaorange.web.controller.certificate;

import com.yaorange.certificate.domain.NjtUnitConfig;
import com.yaorange.certificate.domain.NjtUserPerfect;
import com.yaorange.certificate.service.INjtUnitConfigService;
import com.yaorange.certificate.service.INjtUserPerfectService;
import com.yaorange.certificate.service.impl.NjtCommonService;
import com.yaorange.common.constant.NjtConstants;
import com.yaorange.common.core.domain.AjaxResult;
import com.yaorange.common.core.domain.entity.SysUser;
import com.yaorange.common.core.domain.model.LoginUser;
import com.yaorange.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/njt/index")
public class IndexController {

    @Autowired
    private NjtCommonService njtCommonService;
    @Autowired
    private INjtUnitConfigService njtUnitConfigService;
    @Autowired
    private INjtUserPerfectService userPerfectService;
    /**
     * 判断用户是否有首页权限,如果有,则返回 首页的标题
     *
     * @return
     */
    @GetMapping("/hasIndexAuth")
    public AjaxResult hasIndexAuth() {
        //1.首先获取登录用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUser.getUser();

        Map<String, Object> map = new HashMap<>();
        //默认权限
        map.put("hasIndex", false);
        //1.1如果是公司职员/管理员,拥有所有权限
        if (NjtConstants.PERSON_TYPE_01.equals(sysUser.getUserType())) {
            String title = njtCommonService.getUnitName(loginUser);
            map.put("title", title);
            map.replace("hasIndex", true);
            return AjaxResult.success(map);
        }
        //1.2如果是监管者
        else if (NjtConstants.PERSON_TYPE_02.equals(sysUser.getUserType())) {
            //判断是否开启了首页权限
            boolean hasHomeAuth = njtCommonService.hasConfigAuth(loginUser, NjtConstants.CONFIG_TYPE_01);
            if (hasHomeAuth) {
                NjtUserPerfect perfect = userPerfectService.selectNjtUserPerfectById(sysUser.getUserId());
                NjtUnitConfig unitConfig = njtUnitConfigService.selectNjtUnitConfigById(perfect.getUnitId());
                map.put("title", unitConfig.getUnit());
                map.replace("hasIndex", true);
                return AjaxResult.success(map);
            }
        }
        //1.3如果是普通用户/监管者未开启首页权限.默认无权限
        return AjaxResult.success(map);
    }

}
